package jobqueue;
import java.util.Date;
import java.util.Vector;


public class JobQueueImpl implements JobQueue{
	private Vector<JobQueueInfo> jobq;
	
	public JobQueueImpl()
	{
		jobq = new Vector<JobQueueInfo>();
	}
	
	@Override
	public synchronized void InsertJob(JobQueueInfo jqi) {
		if(jobq.isEmpty())
			jobq.add(jqi);
		else
		{
			int i = 0;
			while(i < jobq.size() && jobq.get(i).isBefore(jqi.getSchedule()))
				i++;
			jobq.add(i, jqi);
		}
		
	}
	
	public boolean isEmpty() { return jobq.isEmpty(); }
	
	@Override
	public synchronized Vector<JobQueueInfo> RemoveSchedulable() {
		Vector<JobQueueInfo> list = new Vector<JobQueueInfo>();
		Date now = new Date();
		while( !jobq.isEmpty() &&  jobq.get(0).isBefore(now))
		{
			list.add(jobq.remove(0));
		}
		return list;
	}


	
}
